//
//  FMDBDataInteractionObject.m
//  XinLiJianKangJiaoYu
//
//  Created by 李文强 on 2018/12/6.
//  Copyright © 2018 sijiaokeji. All rights reserved.
//

#import "FMDBDataInteractionObject.h"

@implementation FMDBDataInteractionObject
//单例
+ (FMDBDataInteractionObject *)manager{
    static FMDBDataInteractionObject *object = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object = [[FMDBDataInteractionObject alloc]  init];
    });
    return object;
}
//查询表是否存在
- (BOOL)existTable:(NSString *)tabName{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:tabName] == NO){
        //数据库不存在
        NSLog(@"NO DB");
        return NO;
    }else{
        //数据库存在
        NSLog(@"exist DB");
        return YES;
    }
}
- (BOOL)createDataBase:(NSString *)baseName{
    NSLog(@"Base Name %@",baseName);
    NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *dbPath = [docuPath stringByAppendingPathComponent:baseName];
    _fileName = dbPath;
    if ([self existTable:_fileName]==NO){
        NSLog(@"!!!dbPath = %@",dbPath);
        //2.创建对应路径下数据库
        FMDBDataInteractionObject *object = [FMDBDataInteractionObject new];
        object.db = [FMDatabase databaseWithPath:dbPath];
        //3.在数据库中进行增删改查操作时，需要判断数据库是否open，如果open失败，可能是权限或者资源不足，数据库操作完成通常使用close关闭数据库
        [object.db open];
        if (![object.db open]) {
            NSLog(@"db open fail");
            return NO;
        }
        //4.数据库中创建表（可创建多张）
        NSString *sql = @"CREATE TABLE IF NOT EXISTS UserInfo ('AccountID' INTEGER PRIMARY KEY AUTOINCREMENT,   'Name' TEXT,   'TrueName' TEXT,   'UserName' TEXT,   'UserSig' TEXT,    'City' TEXT,   'CityID' INTEGER,  'Province' TEXT,   'ProvinceID' INTEGER ,  'School' TEXT, 'SchoolID' INTEGER,   'StageID' TEXT, 'cityVersion' INTEGER)";
        //5.执行更新操作 此处database直接操作，不考虑多线程问题，多线程问题，用FMDatabaseQueue 每次数据库操作之后都会返回bool数值，YES，表示success，NO，表示fail,可以通过 @see lastError @see lastErrorCode @see lastErrorMessage
        BOOL result = [object.db executeUpdate:sql];
        if (result) {
            NSLog(@"create table success");
            
        }
        [object.db close];
        return YES;
    }else{
        //插入数据
        return NO;
    }
    
}
//插入用户信息
- (BOOL)insertUserInfo:(LoginModel *)model{
    FMDBDataInteractionObject *object = [FMDBDataInteractionObject new];
    object.db = [FMDatabase databaseWithPath:_fileName];
    [object.db open];
//    BOOL result = YES;
//    if (result) {
//        NSLog(@"insert into table success");
//        [object.db close];
//        return YES;
//    } else {
//        NSLog(@"数据库插入出错");
//        [object.db close];
        return NO;
//    }
}
//更新用户
- (BOOL)uploadUserInfor:(LoginModel *)model{
    FMDBDataInteractionObject *object = [FMDBDataInteractionObject new];
    object.db = [FMDatabase databaseWithPath:_fileName];
    [object.db open];
//    BOOL result = [object.db executeUpdate:@"update UserInfo set (AccountID,Name,TrueName,UserName,UserSig,City,CityID,Province,ProvinceID,School,SchoolID,StageID,cityVersion) =(?,?,?,?,?,?,?,?,?,?,?,?,?)" withArgumentsInArray:@[[NSNumber numberWithInt:[model.AccountID intValue]],model.Name,model.TrueName,model.UserName,model.UserSig,model.City,[NSNumber numberWithInt:[model.CityID intValue]],model.Province,[NSNumber numberWithInt:[model.ProvinceID intValue]],model.School,[NSNumber numberWithInt:[model.SchoolID intValue]],model.StageID,model.cityVersion]];
//    if (result) {
////        NSLog(@"updata into table success");
//        [object.db close];
//        return YES;
//    } else {
//        NSLog(@"数据库插入出错");
//        [object.db close];
        return NO;
//    }
}
//取出数据
- (LoginModel *)getUserData{
    FMDBDataInteractionObject *object = [FMDBDataInteractionObject new];
    object.db = [FMDatabase databaseWithPath:_fileName];
    [object.db open];
    FMResultSet *result = [object.db executeQuery:@"select * from UserInfo"];
    LoginModel *person;
    while ([result next]) {
//        person = [LoginModel new];
//        person.AccountID = [result stringForColumn:@"AccountID"];
//        person.Name = [result stringForColumn:@"Name"];
//        person.TrueName = [result stringForColumn:@"TrueName"];
//        person.UserName = [result stringForColumn:@"UserName"];
//        person.UserSig = [result stringForColumn:@"UserSig"];
//        person.City = [result stringForColumn:@"City"];
//        person.CityID = [result stringForColumn:@"CityID"];
//        person.Province = [result stringForColumn:@"Province"];
//        person.ProvinceID = [result stringForColumn:@"ProvinceID"];
//        person.School = [result stringForColumn:@"School"];
//
//        person.SchoolID = [result stringForColumn:@"SchoolID"];
//        person.StageID = [result stringForColumn:@"StageID"];
//        person.cityVersion = [result stringForColumn:@"cityVersion"];
//        person.School = [result stringForColumn:@"School"];
    }
    return person;
}
@end
